import { sortBy,compareObject } from '../../../assets/js/util.js'

export default {
	data() {
		return{
        // 默认参数 和 URL
        SRC: '',
        DP: {
          p: '',
          f: '',
          uid: ''
        },
      projects: [],
      region: [],
      // form输入框的胡宽度
      formLabelWidth: '150px',
			/*initWidth 的宽*/
      initWidth:'100%',
      /*当前页显示数量*/
      pageSize: 10,
      /*每页显示的数量*/
      pageSizes: [10, 20, 50, 100],
      /*dataTotal表数量*/
      dataTotal: 0,
      /*分页器*/
      currentPage: 1,
      // 添加或搜索数据
      ruleForm: {
        id:'',
        name:'',
        description:'',
        platName:'',
        platformId:'',
        platformName:''
        // region: "",
        // plat: '',
        // adNum: 0,
        // adKey: '',
        // adValue: '',
        // adDesc: ''
      },
      // 游戏列表数据
      gameList: [
      	// {
        //   value: '权力与荣耀',
        //   label: '权力与荣耀'
        // }, {
        //   value: '九州天空城',
        //   label: '九州天空城'
        // }, {
        //   value: '琅琊榜',
        //   label: '琅琊榜'
        // }
      ],
      // 平台列表
      platList: [
        // {
        //   value: 'iOS',
        //   label: 'iOS'
        // }, {
        //   value: '安卓',
        //   label: '安卓'
        // }, {
        //   value: '全通台',
        //   label: '全通台'
        // }
      ],
      // 表格数据
      tableData: [
        // {
        //   game:"权力与荣耀",
        //   id:0,
        //   name:"渠道名称",
        //   platformName: '所属平台',
        //   platName: "209",
        //   description: "勇士之塔游戏标识"
        // },
        // {
        //   game:"琅琊榜",
        //   id:1,
        //   name:"渠道名称",
        //   platformName: '所属平台',
        //   platName: "use bi_parquet",
        //   description: "impala parquet逻辑库"
        // }
      ],
      // 验证ruleForm
      rules: {
        // region: [
        //   { type: 'array', required: true, message: '请选择游戏名称', trigger: 'change' },
        // ],
        // adNum: [
        //   { required: true, message: 'ID不能为空'},
        //   { type: 'number', message: 'ID必须为数字值'}
        // ],
        // adKey: [
        //   { min: 2, max: 100, message: '长度在 2 到 100个字符', trigger: 'blur' }
        // ],
        // adValue: [
        //   { min: 2, max: 100, message: '长度在 2 到 100个字符', trigger: 'blur' }
        // ],
      },
      // // 修改弹窗出现
      // revampShow: false,
      // // 修改弹窗数据
      // revampForm: {

      // },
      // // 验证 弹窗数据
      // revamp: {

      // },
      // 渠道所属平台
      platformNameList:[],
      tableData:[],
      valueplatformName:''
		}
	},
	methods: {
    successFun(data) {
        this.$message({
            message: data,
            type: 'success'
        });
    },
    errorFun(data) {
        this.$message.error(data);
    },
    // 获取数据 获取工程List 以及页面展示数据
    getProjectData(){
      this.projects = [];
      this.$http.post(`${this.SRC}project/list.do`).then(
        function(res){
          var data = res.body;
          if(data["code"] === 0){
            for(var i=0; i<data['msg'].length; i++){
              var obj = {
                label: data["msg"][i]['projectName'],
                value: data["msg"][i]['id']
              }
              this.projects.push(obj);
            }
          }else{
            this.errorFun(data['msg'])
          }
        }
      )
    },
    // 选择游戏
    projectsChange(val) {
      var p = this.projects;
      for(var i =0; i<p.length; i++){
        if(val === p[i].value){
          this.ruleForm.gameName = p[i].label;
          this.ruleForm.gameId = p[i].value;
        }
      }
    },
		 /*设置初始值*/
    _init(){
      this.$http.post(`${this.SRC}channelconfig/platlist.do?gameId=${this.region}`).then(function(data){
          var data = data.body;
          if(data.code == 0){
              var td = data.msg;
              for(var i of td){
                var obj = {};
                obj.value = i[0];
                obj.label = i[1];
                this.platformNameList.push(obj)
              }
          }else{
            this.errorFun(data['msg'])
          }
      }).catch(function(res){});

      window.addEventListener('resize', function() {
        /*修改表格的宽度*/
            this.initWidth = document.body.clientWidth-220+"px";
        }.bind(this));

    },
    /*handleCurrentChange(o){
      console.log(o)
      this.currentPage = o;
      this.commonAjax()
    },*/
    commonAjax(){
      this.$http.post(`${this.SRC}channelconfig/list.do`,{
        gameId: this.region,
        curPage: this.currentPage,
        pageSize: this.pageSize,
        id: this.ruleForm.id,
        name: this.ruleForm.name,
        description: this.ruleForm.description,
        platName: this.ruleForm.platName,
        platformId: this.ruleForm.platformId
      },{
        emulateJSON: true
      }).then(function(data){
          var data = data.body;
          if(data.code == 0){
              this.tableData = data.msg.data;
              for(var i of this.tableData){
                i.id = parseInt(i.id)
              }
              sortBy(this.tableData,"id",true);
              /*console.log(this.tableData)*/
              this.dataTotal = data.msg.totalSize;
          }else{
            this.errorFun(data['msg'])
          }
      }).catch(function(res){});      
    },
    /*分页*/
    handleSizeChange(val) {
        this.pageSize = val;
        this.commonAjax();
    },
    handleCurrentChange(val) {
        this.currentPage = val;
        this.commonAjax();
    },
    // plFn(o){
    //   this.ruleForm.platformId = o;
    // },
    // // 修改用户
    // userEdit(index, row){
    //   this.revampForm = row;
    //   this.revampForm.password = '';
    //   this.userShow = true;
    // },
    // 编辑权限
    edit(index, row) {
      this.$http.post(`${this.SRC}channelconfig/modify.do`,{
        gameId: row.gameId,
        id: row.id,
        name: row.name,
        description: row.description,
        platName: row.platName,
        platformId: row.platformId
      },{
        emulateJSON: true
      }).then(function(data){
           var data = data.body;
          if(data.code == 0){
              this.successFun(data['msg']);
          }else{
            this.errorFun(data['msg'])
          }
      }).catch(function(res){});
    },
    // 删除用户
    handleDelete(index, row) {
      this.$http.post(`${this.SRC}channelconfig/delete.do`,{
        id: row.id
      },{
        emulateJSON: true
      }).then(function(data){
           var data = data.body;
          if(data.code == 0){
              this.tableData = this.tableData.filter(function(v){
                return v.id !=row.id
              })
              this.successFun(data['msg']);
              this.dataTotal = this.dataTotal-1
          }else{
            this.errorFun(data['msg'])
          }
      }).catch(function(res){});        
    },
    submitForm(formName) {
      var that = this;

      var p = new Promise(function(resolve,reject){
        that.$http.post(`${that.SRC}channelconfig/add.do`,{
            gameId: that.region,
            id: that.ruleForm.id,
            name: that.ruleForm.name,
            description: that.ruleForm.description,
            platName: that.ruleForm.platName,
            platformId: that.ruleForm.platformId,
            platformName: that.ruleForm.platformName
          },{
            emulateJSON: true
          }).then(function(data){
            return resolve(data.body)
          })
      });
      p.then(function(dataP){
        that.$http.post(`${that.SRC}channelconfig/list.do`,{
          curPage: that.currentPage,
          pageSize: that.pageSize
        },{
          emulateJSON: true
        }).then(function(data){
            var data = data.body;
            if(dataP.code==0 && data.code ==0){
              that.tableData = data.msg.data;
              for(var i of that.tableData){
                i.id = parseInt(i.id)
              }
              sortBy(that.tableData,"id",true);
              that.dataTotal = data.msg.totalSize;
              that.successFun(dataP['msg']);
              this.$refs[formName].resetFields();
              this.ruleForm.platformId = '';
              console.log(that.tableData)
              this.region = '';
            }else{
              that.errorFun(dataP['msg'])
            }
        }).catch(function(res){});
      })
    },
    resetForm(formName) {
      this.commonAjax()
    },
	},
	mounted() {
    this.SRC = this.$store.getters.src3;
    this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
    this.getProjectData();
    this._init();
    this.commonAjax()
	}
}